<!DOCTYPE html>
<?php
session_start();

?>
<html>
<?php
$_SESSION['LAST_ACTIVITY'] = time(); // update last activity time stamp
?>    
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <title>Botany Dichotomous Key Conversion System</title>
        <link rel="stylesheet" type="text/css" href="css/default.css" />
        <link rel="stylesheet" type="text/css" href="css/stepsMenu.css" />
        
        <link rel="stylesheet" type="text/css" href="css/ui-lightness/jquery-ui-1.8.23.custom.css"/>
        
        <script type="text/javascript" src="webforms2/webforms2-p.js"></script> 
        <script type="text/javascript">
         /*Funtion to check for valid image formats */
            function load_image(id,ext){
             
                 if(validateImageExtension(ext) == false){

                      alert("Please only upload .png,.jpg or .gif image ");
                      document.getElementById("imageInput").innerHTML = "<label class='basicInfoTitle'><b>Image :</b><label><input type='file' id='image' name='basicInfoImage' size='45' onChange='load_image(this.id,this.value)'>";
                      document.getElementById(id).focus();
                      return;
                  }
             }//end of load_file

            function validateImageExtension(v)
            {
                 var allowedExtensions = new Array("png","PNG","jpg","JPG","jpeg","JPEG","gif","GIF");
                 for(var ct=0;ct < allowedExtensions.length;ct++)
                 {
                  sample = v.lastIndexOf(allowedExtensions[ct]);
                  if(sample != -1){return true;}
                  }
                 return false;
             }//end of validateExtension
        
        </script>
    </head>
    
    <body>
        
        <script type="text/javascript" src="javascripts/jquery.js"></script>
        <script type="text/javascript" src="javascripts/jquery-ui.js"></script>
        <script type="text/javascript" src="javascripts/ui.js"></script>
       
        
        <?php       
             $dir_unique_name = "KeyConversion".time();
            if(!is_dir("./upload/".$dir_unique_name)){   //this checks to make sure the directory does not already exist
            mkdir("./upload/".$dir_unique_name, 0777, true); //if the directory doesn't exist then make it
            chmod("./upload/".$dir_unique_name, 0777);  //chmod to 777 lets us write to the directory
            }
            $uploaddir='./upload/' . $dir_unique_name .'/';   //lets put the directory into a variable notice added slash on end 

            #echo "The upload dir is $uploaddir";
          if ($_FILES["file"]["error"] > 0)
            {
            #echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
            }
          else
            {
        #    echo "Upload: " . $_FILES["file"]["name"] . "<br />";
        #    echo "Type: " . $_FILES["file"]["type"] . "<br />";
        #    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
        #    echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";

               $filename =  $_FILES["file"]["name"];
                 //echo "\n This is filename = ".$filename;
                $new_filename = str_replace(" ", "", $filename);
                 //echo "\n This is new filename = ".$new_filename;
            if (file_exists($uploaddir . $_FILES["file"]["name"]))
              {
              #echo $_FILES["file"]["name"] . " already exists. ";
              }
            else
              {
              
              //move_uploaded_file($_FILES["file"]["tmp_name"],
              //$uploaddir . $_FILES["file"]["name"]);
              #echo "Stored in: " . $uploaddir . $_FILES["file"]["name"];
                move_uploaded_file($_FILES["file"]["tmp_name"],
                $uploaddir . $new_filename);
              }
            }
               
              
               exec("./upload.cgi filename=$new_filename output=$uploaddir");
        ?> 
        
        <?php include("includes/header.php"); ?>
        
       <div id="page-wrap2">
            <div id="main-content">
                <div id="left-col">
                    <?php 
                        if(file_exists($uploaddir."error.txt")){
                            include("includes/menuError.php"); 
                        }
                        else{
                            include("includes/menuBasicinfo.php"); 
                        }
                    ?>
                </div><!-- end of left-col -->
                
        <div id = "right-col">
        <?
        if(file_exists($uploaddir."error.txt"))
        {
            $errorpage = file_get_contents($uploaddir."error.txt");
            echo "<h3>Error</h3>";
            echo "<fieldset id=\"errorField\">";
            echo "<b>".$errorpage."</b><br><br>";
            echo "<a href='index.php'>Try Again</a>";
            echo "</fieldset>";
            foreach(glob($uploaddir . '/*') as $file) {
                if(is_dir($file))
                    rrmdir($file);  
                else
                    unlink($file);
            }
            rmdir($uploaddir);
            }
        else {
        ?>            
            <h3>Step 2: Enter basic information</h3>
            <form action="AddImages.php" method="post" enctype="multipart/form-data">
                <fieldset id="basicInfoField">
                    <p class="instruction">Please enter basic information for the dichotomous key. Required fields are indicated by <font color="red" font-weight="bold">*</font>.</p>
                   <!-- <fieldset id="basicInfo">-->
                    <label class="basicInfoTitle" id="required"><b>Title:</b></label> 
                    <input type="text" id="title" name="basicInfoTitle" size="45" required/><br /><br />
                    <label class="basicInfoTitle"><b>Author:</b></label> 
                    <input type="text" id="author" name="basicInfoAuthor" size="45" /><br /><br />
                    <label class="basicInfoTitle"><b>Date:</b></label>
                    <input type="text" id="date" name="basicInfoDate" size="45"/><br /><br />
                   <!-- <b>Date HTML5:</b>
                    <input type="date" id="date2" name="basicInfoDate2" size="45"/><br /><br />
                   --> 
                   <div id="imageInput">
                    <label class="basicInfoTitle"><b>Image :</b></label>
                    <input type="file" id="image" name="basicInfoImage" size="45" onChange="load_image(this.id,this.value)"/><br /><br />            
                    </div>
                  <!-- </fieldset> -->
                    <div style="text-align:center;"><br /><br />
                        <input type="hidden" name="output" value=<?php echo $uploaddir ?> /> <br />                                
                        <input type="submit" class="next" value="Next"/> <br /><br />
                    </div>
                   
                 </fieldset> 
            </form>
            <?}?>
        </div><!-- end of right-col -->
        <div class="clear"></div> <!-- all the content inside main-content div element is floated, need to clear float...
                                             because height of main-content does not know how tall it would be -->
        </div><!-- end of main-content --> 
        </div>  <!-- End of page-wrap -->  
        <?php include("includes/footer.php"); ?>
        
    </body>
</html>
